let isSending = false; /* 防止多次请求 */
function request(url, method, data, format = 'application/json',) {
  return new Promise((resolve, reject) => {
    let xhr = new XMLHttpRequest();
    if(isSending == true) {
      xhr.abort();
    }
    isSending = true;
    xhr.timeout = 4000;
    xhr.responseType = 'json';
    xhr.open(method, url, true);
    xhr.setRequestHeader("Content-type", format);
    method == 'POST' ? xhr.send(data) : xhr.send();
    xhr.onreadystatechange = function(){
      //判断readyState
      if(this.readyState == 4 && this.status == 200){
        isSending = false;
        resolve(this.response);
      }
    }
    xhr.ontimeout = function() {
      reject('请求超时');
    }
    xhr.onerror = function() {
      reject('加载失败');
    }
  });
}